//
//  NewFeatureViewController.swift
//  WeoboWithSteveLi
//
//  Created by SteveLi on 2017/10/4.
//  Copyright © 2017年 李军桦. All rights reserved.
//

import UIKit
import SnapKit
/// 可重用 cellId
private let WBNewFeatureViewCellId = "WBNewFeatureViewCellId"
/// 新特性图像的数量
private let WBNewFeatureImageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    //懒加载属性，必须在控制器实例化之后才会创建
    //MARK： -构造函数
    init() {
        // super 指定的构造函数
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        
        //构造函数完成之后，内部属性才会被创建
        super.init(collectionViewLayout: layout)
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //ios7.0推荐要隐藏状态栏，可以每个控制器分别设置,默认是NO
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellId)
    }
    // MARK: UICollectionViewDataSource
    //每个分组中格子的数量
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBNewFeatureImageCount
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WBNewFeatureViewCellId, for: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
    //ScrollView停止滚动方法
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            //在最后一页才调用方法
            //根据contentOfSet 计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        //判断是否是最后一页
        if page != WBNewFeatureImageCount - 1 {
            return
        }
        //Cell播放动画
       let cell = collectionView?.cellForItem(at: NSIndexPath(item: page, section: 0) as IndexPath) as!NewFeatureCell
        cell.showButtonAnim()
    }
    
}

//MARK: - 新特性cell
class NewFeatureCell:UICollectionViewCell {
    //图像属性
    var imageIndex:Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            //影藏按钮
            startButton.isHidden = true
     
        }
    }
    
    //显示按钮动画
    /// 点击开始体验按钮
    @objc func clickStartButton() {
        print("开始体验")
        NotificationCenter.default.post(name: WBSwitchRootViewControllerNotification, object: nil)
    }
    
    func showButtonAnim() {
        startButton.isHidden = false
        startButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        startButton.isUserInteractionEnabled = false
        UIView.animate(withDuration: 1.6,//动画时长
                       delay: 0,//延时时间
                       usingSpringWithDamping: 0.6,//弹力系数， 0~1,越小越弹
                       initialSpringVelocity: 10, //初始速度，模拟重力加速度
                       options: [],//动画选项
                       animations: {
                        self.startButton.transform = CGAffineTransform.identity
        }) { (_) in
            self.startButton.isUserInteractionEnabled = true
        }
    }
    
    //frame的大小是 layout.itemSize指定的
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUpUI(){
        //1.添加控件
        addSubview(iconView)
        addSubview(startButton)
        //2.指定位置
        iconView.frame = bounds
       startButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom).multipliedBy(0.7)
        }
        //3.监听方法
        startButton.addTarget(self, action: #selector(clickStartButton), for: .touchUpInside)
    }
    
    
    //MARK: --懒加载控件
    /// 图像
    lazy var iconView: UIImageView = UIImageView()
    //开始体验按钮
    lazy var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.red, backImageName: "new_feature_finish_button")
}



